package org.muxiao.seckill.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ 配置类
 */
@Configuration
public class RabbitMQSecKillConfig {

    //定义消息队列和交换机
    public static final String QUEUE_SECKILL = "queue_seckill";
    public static final String EXCHANGE_SECKILL = "exchange_seckill";
    //路由
    private static final String ROUTING_SECKILL_KEY = "seckill.#";

    /**
     * 创建队列
     *
     * @return
     */
    @Bean
    public Queue queue_seckill() {
        return new Queue(QUEUE_SECKILL);
    }

    /**
     * 创建 topic 交换机
     *
     * @return
     */
    @Bean
    public TopicExchange exchange_seckill() {
        return new TopicExchange(EXCHANGE_SECKILL);
    }

    /**
     * 绑定队列到交换机,并指定路由
     *
     * @return
     */
    @Bean
    public Binding binding_secKill() {
        return BindingBuilder.bind(queue_seckill()).to(exchange_seckill()).with(ROUTING_SECKILL_KEY);
    }
}
